package com.wx.core.process.message;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wx.commons.MessageConstant;
import com.wx.commons.ResultConstant;
import com.wx.core.process.BaseProcess;
import com.wx.core.process.BaseResult;
import com.wx.core.process.ProcessResult;
import com.wx.core.vo.MsgRequest;

/**
 * @desc 将所有的事件类消息首先推送到这个类
 * @author gavin
 * @date 2015-7-3
 * @time 下午3:33:53
 * @version 1.0
 */
public class EventMessageProcess extends BaseProcess {

	@Override
	public BaseResult process(HttpServletRequest req, HttpServletResponse res, MsgRequest msgReq) {
		ProcessResult result = new ProcessResult();
		try {
			if (eventMap.containsKey(msgReq.getEvent())) {
				BaseProcess p = eventMap.get(msgReq.getEvent());
				result = (ProcessResult) p.process(req, res, msgReq);
				if (MessageConstant.EVENT_TYPE_SUBSCRIBE.equals(msgReq.getEvent())) {
					p = eventMap.get(MessageConstant.EVENT_TYPE_SUBSCRIBE);
				} else if (MessageConstant.EVENT_TYPE_UNSUBSCRIBE.equals(msgReq.getEvent())) {
					p = eventMap.get(MessageConstant.EVENT_TYPE_UNSUBSCRIBE);
				} else {
					p = eventMap.get(msgReq.getEvent());
				}
				result = (ProcessResult) p.process(req, res, msgReq);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(ResultConstant.FAILUER);
			result.setErrmsg(e.getMessage());
		}
		result.setCode(ResultConstant.SUCCESS);
		return result;
	}

	/**
	 * 事件类消息中转集合
	 */
	private Map<String, BaseProcess> eventMap;

	public Map<String, BaseProcess> getEventMap() {
		return eventMap;
	}

	public void setEventMap(Map<String, BaseProcess> eventMap) {
		this.eventMap = eventMap;
	}
}
